package com.css.devops.service.jenkins;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import com.css.devops.component.JenkinsBasicComponent;
import com.css.devops.constants.DevopsConstants;
import com.css.devops.domain.BuildResult;
import com.css.devops.domain.BuildState;
import com.css.devops.domain.PipelineMonitor;
import com.css.devops.domain.vo.PipelineMonitorQueryCriteria;
import com.css.devops.service.DevopsPipelineMonitor;
import com.css.devops.utils.JsonMapperUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.Lists;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpResponse;
import lombok.extern.slf4j.Slf4j;

/**
 * pipeline 构建
 */
@Slf4j
@Service("jenkinsPipelineMonitorService")
public class JenkinsPipelineMonitorService implements DevopsPipelineMonitor {

	@Autowired
	private JenkinsBasicComponent jenkinsBasicComponent;
	
	/**
	 * 查询pipeline构建列表
	 * @param pipelineMonitorQueryCriteria
	 * @return
	 */
    @Override
	public List<PipelineMonitor> queryPipelineMonitor(PipelineMonitorQueryCriteria pipelineMonitorQueryCriteria) {
    	List<PipelineMonitor> pipelineMonitors = Lists.newArrayList();
        try {
            String endpoint = String.format(DevopsConstants.JENKINS_PIPELINE_MONITOR_ENDPOINT, pipelineMonitorQueryCriteria.getPipeline(), pipelineMonitorQueryCriteria.getBranch(), pipelineMonitorQueryCriteria.getBuildNumber());
            HttpResponse response = jenkinsBasicComponent.getRequest(endpoint);
            
            if (response.getStatus() == HttpStatus.OK.value()) {
            	JsonNode builds = JsonMapperUtils.getInstance().readTree(response.body());
            	builds.forEach(node->{
            		PipelineMonitor pipelineMonitor = buildPipelineMonitor(node);
                    pipelineMonitors.add(pipelineMonitor);
            	});
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return pipelineMonitors;
    }
    
    /**
     * 构建pipeline的各个阶段
     * @param node
     * @return
     */
    @Override
	public PipelineMonitor buildPipelineMonitor(JsonNode node) {
    	PipelineMonitor pipelineMonitor = new PipelineMonitor();
		String id = node.path("id").asText();
		String displayName = node.path("displayName").asText();
        long durationInMillis = node.path("durationInMillis").asLong();
        String startTime = node.path("startTime").asText();
        String state = node.path("state").asText();
        String result = node.path("result").asText();
        String type = node.path("type").asText();

        pipelineMonitor.setId(id);
        pipelineMonitor.setDisplayName(displayName);
        pipelineMonitor.setDuration(durationInMillis);
        pipelineMonitor.setStartTime(DateUtil.parse(startTime));
        pipelineMonitor.setState(BuildState.getBuildState(state));
        pipelineMonitor.setResult(BuildResult.getBuildResult(result));
        pipelineMonitor.setType(type);
        return pipelineMonitor;
    }
}


